﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using SubSonic;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class ProductWithCategoryRepository<TCollection, TItem> : MedianamikRepository<TCollection, TItem>,
            IProductRepository<TItem>
        where TItem : Interfaces.IProduct, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, TItem>, IAbstractList, new()
    {

        public ProductWithCategoryRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public IList<TItem> GetProductsByCategory(Guid categoryId)
        {
            throw new NotImplementedException();
        }


        public PagedCollection<TItem> PagedWithFilters(CultureInfo culture, ContentState contentState,
            ActiveStatus status ,int pageIndex, int pageSize, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords)
        {
            var cultureList = getCultureList(culture);


            return new PagedCollection<TItem>(
                SPs.GetGroupedProduct(pageSize, pageIndex, isActive(status), parentNodeId, typeId, brand, categories, keywords, cultureList, (int)contentState).ExecuteTypedList<TItem>(),
                GetProductCount(culture,contentState,status, parentNodeId, typeId, brand, categories, keywords));
        }


        private bool? isActive(ActiveStatus status)
        {
            bool? isActive;

            switch (status)
            {
                case ActiveStatus.All:
                     isActive = null;
                    break;
                case ActiveStatus.Activated:
                    isActive = true;
                    break;
                default:
                    isActive = false;
                    break;
            }

            return isActive;
        }


        public int GetProductCount(CultureInfo culture,ContentState contentState,
            ActiveStatus status, Guid? parentNodeId, Guid? typeId, Guid? brand, Guid? categories, string keywords)
        {
            var cultureList = getCultureList(culture);

            using (var reader = SPs.GetGroupedProductCount(isActive(status), parentNodeId, typeId, brand, categories, keywords, (int)contentState, cultureList).GetReader())
            {
                if (reader.Read())
                    return reader.GetInt32(0);
            }

            throw new Exception("SP neew to return a records");
        }

        private string getCultureList(CultureInfo culture)
        {
            var cultureNames = ConfigManager.GetEditionCultures().Select(c => c.CultureName).ToList();
            cultureNames.Remove(culture.Name);
            cultureNames.Insert(0, culture.Name);

            return string.Join(",", cultureNames.ToArray());
        }

    }

    public class ProductWithCategoryRepository :ProductWithCategoryRepository<ProductWithCategoryCollection,ProductWithCategory>
    {
        public ProductWithCategoryRepository(ICultureService cultureService, 
            IContentStateService contentStateService, INodeMapper nodeMapper) 
            : base(cultureService, contentStateService, nodeMapper)
        {
        }
    }
}
